package com.skyjing.tooluitls;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatButton;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.skyjing.toolsuitls.java.base.BaseActivity;
import com.skyjing.toolsuitls.java.tools.AppFile;
import com.skyjing.toolsuitls.java.tools.DoubleClickUtils;
import com.skyjing.toolsuitls.java.tools.FileIntentUitls;
import com.skyjing.toolsuitls.java.tools.ImageUitls;
import com.skyjing.toolsuitls.java.tools.KeyboardUitls;
import com.skyjing.toolsuitls.java.tools.LocationSwitch;
import com.skyjing.toolsuitls.java.tools.LogUtils;
import com.skyjing.toolsuitls.java.tools.NetworkUtils;
import com.skyjing.toolsuitls.kotlin.tools.SingleToast;
import com.skyjing.toolsuitls.java.tools.SpUitls;
import com.skyjing.toolsuitls.java.tools.SpanStrUtils;
import com.skyjing.toolsuitls.java.tools.Utils;
import com.skyjing.toolsuitls.kotlin.tools.StringUitls;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv;
    private AppCompatButton btn;
    private SpUitls SP_UTILS1;
    private SpUitls SP_UTILS2;

    private AppFile file1;
    private AppFile file2;
    private AppCompatButton btnSecondView;
    private AppCompatButton btnTestLocationSwitch;
    private AppCompatButton btnOpen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isOpenKeyListener = true;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.init(this);
        initView();
        SP_UTILS1 = SpUitls.getInstance("9918");
        SP_UTILS2 = SpUitls.getInstance("9527");

        file1 = AppFile.getInstance("file1", AppFile.AppFileAble.APPEND);
        file2 = AppFile.getInstance("file3", AppFile.AppFileAble.PRIVATE);

        SingleToast.Companion.getInstance().show("123", 100);
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.tv);
//        tv.setOnClickListener(this);
        btn = (AppCompatButton) findViewById(R.id.btn);
        btn.setOnClickListener(this);

        SpannableStringBuilder msp = new SpannableStringBuilder();
        msp.append(tv.getText());
        int start = msp.length();
        msp.append("测试下\n");
        int end = msp.length();
        msp.setSpan(new URLSpan("tel:9527"), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)),
                start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setText(msp);
        btnSecondView = (AppCompatButton) findViewById(R.id.btn_second_view);
        btnSecondView.setOnClickListener(this);
        btnTestLocationSwitch = (AppCompatButton) findViewById(R.id.btn_test_location_switch);
        btnTestLocationSwitch.setOnClickListener(this);
        LogUtils.e("dsfsfsdf");
        btnOpen = (AppCompatButton) findViewById(R.id.btn_open);
        btnOpen.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv: {
                DoubleClickUtils.isDoubleClick(new DoubleClickUtils.DoubleClickListener() {
                    @Override
                    public void positive() {
                        Log.e("TAG", "tv positive: ");
                        tv.setText(SpanStrUtils.get()
                                .append("第一个").setForegroundColor(Color.LTGRAY)
                                .append("百度").setUrl("https://www.baidu.com").create()
                        );
                    }

                    @Override
                    public void negative() {
                        Log.e("TAG", "tv negative: ");
                    }
                });
            }
            break;
            case R.id.btn: {

                DoubleClickUtils.isDoubleClick(new DoubleClickUtils.DoubleClickListener() {
                    @Override
                    public void positive() {
                        Log.e("TAG", "btn positive: ");
                        tv.setText(SpanStrUtils.get()
                                        .append("第一个")
                                        .setFontSize(50)
                                        .setForegroundColor(Color.LTGRAY)
                                        .appendLines("百度")
                                        .setUnderline()
                                        .setForegroundColor(Color.GREEN)
                                        .setUrl("https://www.baidu.com")
                                        .setClickSpan(new ClickableSpan() {
                                            @Override
                                            public void onClick(@NonNull View widget) {
                                                SingleToast.Companion.getInstance().show("天天向上", 10000);

                                                file1.write("天天向上");
                                                file2.write("day day up");

                                                LogUtils.e(file1.read());
                                                LogUtils.e(file2.read());

                                            }
                                        })
                                        .append("打个电话")
                                        .setItalic()
                                        .setLeadingMargin(30, 30)
                                        .setUrl("tel:123")
                                        .setForegroundColor(Color.parseColor("#991855"))
                                        .setClickSpan(new ClickableSpan() {
                                            @Override
                                            public void onClick(@NonNull View widget) {
                                                SingleToast.Companion.getInstance().show("day day up", 10000);
                                                LogUtils.e(SP_UTILS1.getInt("tag"));
                                                LogUtils.e(SP_UTILS2.getInt("test"));
                                                LogUtils.e(file1.read());
                                                LogUtils.e(file2.read());

                                                file1.write("苟利生死以");
//                                        file2.write("[{&quot;allSongIndex&quot;:0,&quot;duration&quot;:0,&quot;id&quot;:118,&quot;name&quot;:&quot;为了你, 为了我&quot;,&quot;pictureID&quot;:16,&quot;playlistID&quot;:0,&quot;singerName&quot;:&quot;Beyond&quot;,&quot;sortLetters&quot;:&quot;W&quot;,&quot;url&quot;:&quot;/storage/ADB1-EB06/为了你, 为了我.mp3&quot;},{&quot;allSongIndex&quot;:0,&quot;duration&quot;:0,&quot;id&quot;:121,&quot;name&quot;:&quot;Just the Way You Are&quot;,&quot;pictureID&quot;:19,&quot;playlistID&quot;:0,&quot;singerName&quot;:&quot;Bruno Mars&quot;,&quot;sortLetters&quot;:&quot;J&quot;,&quot;url&quot;:&quot;/storage/ADB1-EB06/Bruno Mars - Just the Way You Are.mp3&quot;},{&quot;allSongIndex&quot;:0,&quot;duration&quot;:0,&quot;id&quot;:117,&quot;name&quot;:&quot;I Knew You Were Trouble&quot;,&quot;pictureID&quot;:17,&quot;playlistID&quot;:0,&quot;singerName&quot;:&quot;Madilyn Bailey&quot;,&quot;sortLetters&quot;:&quot;I&quot;,&quot;url&quot;:&quot;/storage/ADB1-EB06/I Knew You Were Trouble.mp3&quot;}]");

                                                LogUtils.e(file1.read());
                                                LogUtils.e(file2.read().replaceAll("&quot;", "\""));
                                            }
                                        }).create()
                        );
//                        com.skyjing.tooluitls.SpanStrUtils.

//                        SP_UTILS1.put("tag", 9918);
//                        SP_UTILS2.put("test", 9527);
////                        write("to be no1");
//                        file1.write("文件1");
//                        file2.write("文件2");
//                        LogUtils.e(getSDPath());
                        startActivity(FileIntentUitls.htmlFileIntent("https://www.baidu.com"));
                    }

                    @Override
                    public void negative() {
                        Log.e("TAG", "btn negative: ");
                    }
                });
            }
            break;
            case R.id.btn_second_view: {
                startActivitys(new Intent(this, SecondActivity.class));
            }
            break;
            case R.id.btn_test_location_switch:
                LocationSwitch.get().showDialogAndResult("111", "y", "n", new LocationSwitch.ControlGpsListener() {
                    @Override
                    public void isOpened() {
                        LogUtils.e("打开");
                    }

                    @Override
                    public void ok(Intent intent) {
                        LogUtils.e("跳转系统");
                        startActivityForResult(intent, 0);
                    }
                });
                break;
            case R.id.btn_open:
//                NetworkUtils.setMobileDataEnabled(true);
                KeyboardUitls.showToggleSoftInput();
                break;
        }
    }

    public String read() {
        try {
            FileInputStream inStream = this.openFileInput("message.txt");
            byte[] buffer = new byte[1024];
            int hasRead = 0;
            StringBuilder sb = new StringBuilder();
            while ((hasRead = inStream.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, hasRead));
            }

            inStream.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void write(String msg) {
        // 步骤1：获取输入值
        if (msg == null) return;
        try {
            // 步骤2:创建一个FileOutputStream对象,MODE_APPEND追加模式
            FileOutputStream fos = openFileOutput("message.txt",
                    MODE_APPEND);
            // 步骤3：将获取过来的值放入文件
            fos.write(msg.getBytes());
            // 步骤4：关闭数据流
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
